package com.example.swipytabs;



import android.app.ListFragment;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Tab3Fragment extends Fragment implements OnItemClickListener {
    public Tab3Fragment() {
    }

    ListView listViewMedia; 
    String[] pinyinArray, chineseCharacters, audioFiles;
    MediaPlayer mPlayer = new MediaPlayer();
    TextView textViewStatus;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	View v = inflater.inflate(R.layout.tab3layout, null);
    	
    	//get string array with Chinese characters
    	chineseCharacters = getResources().getStringArray(R.array.chinesecharacters1_3);
    	//get string array with pinyin characters
    	pinyinArray = getResources().getStringArray(R.array.pinyin1_3);
    	audioFiles = getResources().getStringArray(R.array.audiofiles1_3);
    	
    	
    	listViewMedia=(ListView)v.findViewById(R.id.listView1);
    	listViewMedia.setAdapter(new ArrayAdapter<String>(getActivity().getBaseContext(),
    				android.R.layout.simple_list_item_multiple_choice,
    				chineseCharacters));
	
    	listViewMedia.setOnItemClickListener(this);
    	
    	//text field to display character data at the top of the screen
    	textViewStatus=(TextView)v.findViewById(R.id.textViewStatus);
    	
    	//TextView textView = new TextView(getActivity());
        //textView.setGravity(Gravity.CENTER);
        //Bundle args = getArguments();
        //textView.setText("I'm in Tab1Fragment Now!!!!");
        return v;
    }


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int listPosition, long arg3) {
		// TODO Auto-generated method stub
		//Toast.makeText(getActivity(), Integer.toString(listPosition)+"Clicked", Toast.LENGTH_SHORT ).show(); 
		if (mPlayer.isPlaying() == true){
			Toast.makeText(getActivity(), "Stopping "+(CharSequence) listViewMedia.getItemAtPosition(listPosition), Toast.LENGTH_SHORT).show();
			textViewStatus.setText("Stopping "+(CharSequence) listViewMedia.getItemAtPosition(listPosition));
			mPlayer.stop();
			//mPlayer.release();
			if (listPosition > 0)
				playListItem( listPosition);
		}
		else {
			//Toast.makeText(getApplicationContext(), "Starting "+(CharSequence) listViewMedia.getItemAtPosition(listPosition), Toast.LENGTH_SHORT).show();
			mPlayer.release();
			textViewStatus.setText(pinyinArray[listPosition].toString());
			playListItem( listPosition);
		}
	}
	
	private void playListItem(int listPosition) {
		// TODO Auto-generated method stub
		int mediaID;
		
		//String audio = audioFiles[listPosition].toString();
		Uri path = Uri.parse("android.resource://com.example.swipytabs/raw/"+audioFiles[listPosition].toString());
		//Uri path = Uri.parse("android.resource://com.example.swipytabs/raw/bu");

		Toast.makeText(getActivity(),"Postion ="+Integer.toString(listPosition)+ " Playing "+ path.toString(), Toast.LENGTH_SHORT).show();

		//mPlayer = MediaPlayer.create(getActivity(), mediaID);
		mPlayer = MediaPlayer.create(getActivity(), path);
		mPlayer.start();
	}
}
